/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-01 14:11:12
 * @Description: file content
 * @FilePath: \miniapp\dwq\pages\check\busResult\busResult.js
 */

import { getSchedule, getOperator } from '../../../api/checkBus/checkBus'
import { computingTime } from '../../../utils/util'
import { _t } from '../../../i18n/base'
Page({
  data: {
    busInfoList: [],
    route: {},
    lang: {},
    language: '',
    isNoList: false,
  },
  // 获取巴士列表数据
  async getBusInfoList(e) {
    let { result } = await getSchedule(e)
    let listData = result.data
    let res = await Promise.all(
      listData.map(async (item) => {
        item.balanceTime = computingTime(item.depTime, item.arrTime)
        let { result } = await getOperator({ opt_id: item.opt_id })
        if (result) {
          item.Opt_ChsName = result[0].Opt_ChsName
          item.Opt_chtName = result[0].Opt_chtName
          item.opt_EngName = result[0].opt_EngName
        }
        return item
      })
    )
    if (res.length) {
      this.setData({
        busInfoList: res,
      })
    } else {
      this.setData({
        isNoList: true,
      })
    }
  },
  // 获取巴士公司名称
  getBusOptName() {
    this.data.busInfoList.map(async (item) => {
      let { result } = await getOperator(item.opt_id)
      if (result) {
        item.Opt_ChsName = result[0].Opt_ChsName
        item.Opt_chtName = result[0].Opt_chtName
        item.opt_EngName = result[0].opt_EngName
      }
    })
    wx.nextTick(() => {
      this.setData({
        test: this.data.busInfoList,
      })
    })
  },
  // 复制
  copyLinkHandle() {
    let urlObj = {
      zh: 'https://www.gobybus.hk/bus/zh/?currency=HKD',
      tw: 'https://www.gobybus.hk/bus/zh/?currency=HKD',
      en: 'https://www.gobybus.hk/bus/en/?currency=HKD',
    }
    let language = wx.getStorageSync('language')
    let url = ''
    url = urlObj[language]
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  onLoad(e) {
    // 保存路线信息
    let info = JSON.parse(e.info)
    this.setData({
      route: info,
      lang: _t(),
      language: wx.getStorageSync('lang'),
    })
    wx.setNavigationBarTitle({
      title: this.data.lang['巴士查询'],
    })
    this.getBusInfoList(info)
  },
})
